<template>
  <div class="bigbox">
    <div class="top">
      <el-form
        ref="form"
        :model="form"
        label-width="40px"
        label-position="left"
      >
        <el-row :gutter="20">
          <el-col :span="6">
            <el-form-item label="日期">
              <el-date-picker
                size="small"
                v-model="form.dateTime"
                type="date"
                placeholder="选择日期"
                format="yyyy-MM-dd"
                value-format="yyyy-MM-dd"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="18" style="text-align: right">
            <el-form-item>
              <Twobtn @chaxun="getList" @chongzhi="reset"></Twobtn>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div class="main">
      <TopTile @command="command" @daochu="daochu" @shuaxin="getList"></TopTile>
      <div style="width: 100%; height: 50%">
        <nt-echart :loading="loading" :options="barOptions"></nt-echart>
      </div>
      <nt-table
        v-loading="loading"
        height="45%"
        :issize="issize"
        :tableData="List"
        :tableConfig="tableConfig"
        :default-sort="{ prop: 'companyName', order: 'descending' }"
      >
      </nt-table>
    </div>
  </div>
</template>

<script>
import ntEchart from '@/components/nt-echart.vue'
import ntTable from '@/components/nt-table'
import dayjs from 'dayjs'
import TopTile from '@/components/TopTitle/index.vue'
import { getUserDayLoadListApi } from '@/api/Intelligence-maintenance/Auxiliary-analysis/User-dayload'
import { exportexcel } from '@/utils/excel'
export default {
  components: { ntEchart, ntTable, TopTile },
  name: 'UserDayload',
  data() {
    return {
      form: {
        dateTime: dayjs().format('YYYY-MM-DD')
      },
      issize: 'middle',
      loading: false,
      List: [],
      tableConfig: [
        {
          label: '用电单位',
          prop: 'companyName',
          sortable: true
        },
        {
          label: '平均负荷(kW)',
          prop: 'avgLoad',
          align: 'right'
        },
        {
          label: '最大负荷(kW)',
          prop: 'maxLoad',
          align: 'right'
        },
        {
          label: '最大负荷发生时间',
          prop: 'maxLoadTime',
          align: 'center'
        },
        {
          label: '平均负荷率(%)',
          prop: 'avgLoadRate',
          align: 'right',
          sortable: true
        },
        {
          label: '最小负荷(kW)',
          prop: 'minLoad',
          align: 'right'
        },
        {
          label: '最小负荷发生时间',
          prop: 'minLoadTime',
          align: 'center'
        },
        {
          label: '最小负荷率(%)',
          prop: 'minLoadRate',
          align: 'right',
          sortable: true
        }
      ]
    }
  },
  computed: {
    barOptions() {
      const data = {
        type: 'bar',
        data: this.List.map((i) => {
          return [i.companyName, i.avgLoadRate]
        })
      }
      return {
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow'
          }
        },
        xAxis: {
          type: 'category'
        },
        yAxis: {
          name: '平均负荷率(%)',
          nameTextStyle: {
            fontSize: 25,
            fontWeight: 'bold',
            color: 'black',
            lineHeight: 56
          },
          type: 'value'
        },
        series: data
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    command(val) {
      if (val === '默认') {
        this.issize = 'medium'
      } else if (val === '中等') {
        this.issize = 'small'
      } else {
        this.issize = 'mini'
      }
    },
    // 重置
    reset() {
      this.form.dateTime = dayjs().format('YYYY-MM-DD')
    },
    async getList() {
      this.loading = true
      const res = await getUserDayLoadListApi({
        dateTime: this.form.dateTime
      })
      this.List = res.data.list
      this.loading = false
    },
    daochu() {
      exportexcel(
        '/pio-microservice-biz/pio/load/analysis/day/user/exportExcel',
        { dateTime: this.form.dateTime },
        '用户日负荷分析'
      )
    }
  }
}
</script>

<style lang="scss" scoped>
.bigbox {
  display: flex;
  flex-direction: column;
  background-color: #f1f1f1;
  padding: 10px;
  height: 100%;
  .top {
    background-color: #fff;
    padding: 20px;
    box-sizing: border-box;
    height: 80px !important;
    width: 100% !important;
    margin-bottom: 10px;
  }
  .main {
    flex: 1;
    background-color: #fff;
    padding: 20px;
  }
}
</style>
